package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CopyrightAuctionRecord;
import com.sc.nft.entity.dto.CopyrightMarketAuctionDTO;
import com.sc.nft.entity.vo.CopyrightAuctionAdminPageVO;
import com.sc.nft.entity.vo.CopyrightAuctionRecordAdminListVO;
import com.sc.nft.entity.vo.CopyrightAuctionRecordUserListVO;
import com.sc.nft.enums.AcutionParticipationTypeEnum;
import com.sc.nft.enums.AuctionAdminStatusEnum;
import com.sc.nft.mapper.CopyrightAuctionRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author：LiGen
 * @Package：com.sc.nft.dao
 * @Project：sc_metago
 * @name：CopyrightAuctionDao
 * @Date：2024/9/12 17:39
 * @Filename：CopyrightAuctionDao
 */
@Slf4j
@Repository
public class CopyrightAuctionRecordDao extends ServiceImpl<CopyrightAuctionRecordMapper, CopyrightAuctionRecord> {

    /**
     * 当前用户拍卖记录
     * @param userId
     * @param mp
     * @return
     */
    public IPage<CopyrightAuctionRecordUserListVO> copyrightAuctionRecordUserList(Long userId, Page mp) {
            return baseMapper.copyrightAuctionRecordUserList(mp, userId);
    }


    /**
     * 后台拍卖记录
     * @param mp
     * @param firstTitle
     * @return
     */
    public IPage<CopyrightAuctionRecordAdminListVO> copyrightAuctionRecordAdminUserList( Page mp,String firstTitle) {
        IPage<CopyrightAuctionRecordAdminListVO> copyrightAuctionRecordAdminListVOIPage = baseMapper.copyrightAuctionRecordAdminUserList(mp, firstTitle);
        List<CopyrightAuctionRecordAdminListVO> records = copyrightAuctionRecordAdminListVOIPage.getRecords();
        if (ObjectUtil.isNotNull(records)) {
            records.forEach(record -> {
                if (new Date().before(record.getAuctionStartTime())) {
                    record.setAuctionStatus(AuctionAdminStatusEnum.NOT_START);
                    record.setAuctionStatusName(AuctionAdminStatusEnum.NOT_START.getDescription());
                } else if (new Date().after(record.getAuctionEndTime())) {
                    record.setAuctionStatus(AuctionAdminStatusEnum.END_AUCTION);
                    record.setAuctionStatusName(AuctionAdminStatusEnum.END_AUCTION.getDescription());
                } else {
                    record.setAuctionStatus(AuctionAdminStatusEnum.START_AUCTION);
                    record.setAuctionStatusName(AuctionAdminStatusEnum.START_AUCTION.getDescription());
                }
            });
        }
        return copyrightAuctionRecordAdminListVOIPage;
    }



    /**
     * 获取当天
     * @param collectionId
     * @return
     */
    public CopyrightAuctionRecord getByCollectionAndToday(Long collectionId) {
        LambdaQueryWrapper<CopyrightAuctionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopyrightAuctionRecord::getCollectionId,collectionId);
        queryWrapper.eq(CopyrightAuctionRecord::getHaveResult,Boolean.FALSE);
        queryWrapper.last(" AND DATE(auction_start_time) = CURDATE()");
        return this.getOne(queryWrapper);
    }
    /**
     * 获取明天
     * @param collectionId
     * @return
     */
    public CopyrightAuctionRecord getByCollectionAndTomorrow(Long collectionId) {
        LambdaQueryWrapper<CopyrightAuctionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopyrightAuctionRecord::getCollectionId,collectionId);
        queryWrapper.eq(CopyrightAuctionRecord::getHaveResult,Boolean.FALSE);
        queryWrapper.last("AND DATE(auction_start_time) = DATE(DATE_ADD(NOW(), INTERVAL 1 DAY))");
        return this.getOne(queryWrapper);
    }

    public boolean updateStartPrice(Long id, Integer startPrice) {
        LambdaUpdateWrapper<CopyrightAuctionRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CopyrightAuctionRecord::getId, id);
        updateWrapper.set(CopyrightAuctionRecord::getStartPrice,startPrice);
        return update(updateWrapper);
    }

    public IPage<CopyrightMarketAuctionDTO> market(Integer pageNo, Integer pageSize,String name) {
        return baseMapper.market(new Page(pageNo, pageSize),name);
    }

    public CopyrightAuctionRecord getNewOne(Long collectionId) {
        LambdaQueryWrapper<CopyrightAuctionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopyrightAuctionRecord::getCollectionId,collectionId);
        queryWrapper.last("order by id desc limit 1 ");
        return this.getOne(queryWrapper);
    }

    public CopyrightAuctionRecord getNewOneByCopyrightAuctionId(Long copyrightAuctionId) {
        LambdaQueryWrapper<CopyrightAuctionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopyrightAuctionRecord::getCopyrightAuctionId,copyrightAuctionId);
        queryWrapper.last("order by id desc limit 1 ");
        return this.getOne(queryWrapper);
    }

    public Integer getAfterTimeAndCollectionId(Long collectionId) {
        LambdaQueryWrapper<CopyrightAuctionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopyrightAuctionRecord::getCollectionId,collectionId);
        queryWrapper.gt(CopyrightAuctionRecord::getAuctionEndTime,new Date());
        return count(queryWrapper);
    }

    public List<CopyrightAuctionRecord> getNotHaveResultList(Date now) {
        LambdaQueryWrapper<CopyrightAuctionRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(CopyrightAuctionRecord::getAuctionStartTime,now);
        queryWrapper.eq(CopyrightAuctionRecord::getHaveResult,Boolean.FALSE);
        return list(queryWrapper);
    }

    public Boolean updateHaveResult(Long id, Boolean haveResult) {
        LambdaUpdateWrapper<CopyrightAuctionRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CopyrightAuctionRecord::getId, id);
        updateWrapper.set(CopyrightAuctionRecord::getHaveResult,haveResult);
        return update(updateWrapper);
    }
}
